package cn.waqwb.controller;

import cn.waqwb.service.UserService;
import cn.waqwb.service.impl.UserServiceImpl;
import cn.waqwb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author JokerQ
 * @date 2020/7/30 - 17:40
 */
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("请求被执行了！");
        String email= request.getParameter("email");
        String nickname=request.getParameter("nickname");
        String password=request.getParameter("password");
        System.out.println(email+","+nickname+","+password);
        UserService userService = new UserServiceImpl();
        User user = new User();
        user.setEmail(email);
        user.setNickname(nickname);
        user.setPassword(password);
        int row = userService.saveUser(user);
        if (row>0){
            HttpSession session = request.getSession();
            session.setAttribute("nickname",user.getNickname());
            session.setAttribute("email",user.getEmail());
            System.out.println("Regist Success");
            request.getRequestDispatcher("register_ok.jsp").forward(request,response);
        } else {
            // 跳转注册界面，并提示错误信息
            request.setAttribute("errorMsg", "注册失败，请稍后再试！");
//            request.getRequestDispatcher("register.jsp").forward(request, response);
            response.sendRedirect("login.jsp");
        }


    }


}
